﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using POS_NEW.DAO.LinQ;
using Infragistics.Win.UltraWinGrid;



namespace POS_NEW
{
    public partial class frmTraHang : Form
    {
        public frmTraHang()
        {
            InitializeComponent();
        }
        public static string Ma_PhieuTraKH = "";
         int DonVi = 0;
        DataTable tbHangHoa = null;
        DataTable tbThanhPham = null;
        DataTable tbCombo = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            dtNgayTra.Focus();
            dtNgayTra.SelectAll();
            loadCombobox();
            loadUltraOption();
            loadGridTraHang();
        }
        private void loadCombobox()
        {
            loadComboboxNgoaiTe();
            loadComboboxHangHoa();
            loadComboboxKhachHang();
        }
        private void loadGridTraHang()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID_HangHoa");
            dt.Columns.Add("ID_ThanhPham");
            dt.Columns.Add("ID_Combo");
            dt.Columns.Add("Ten_HangHoa");
            dt.Columns.Add("SoLo");
            dt.Columns.Add("SoLuong");
            dt.Columns.Add("DonGia");
            dt.Columns.Add("ThanhTien");
            dt.Columns.Add("ID_KhuyenMai");
            dt.Columns.Add("Status");
            dt.Columns.Add("ID_DonVi");
            dt.Columns.Add("Ten_DonVi");
            grdPhieu_TraKH_Chitiet.DataSource = dt;
            grdPhieu_TraKH_Chitiet.DisplayLayout.Bands[0].Columns["ID_HangHoa"].Hidden = true;
            grdPhieu_TraKH_Chitiet.DisplayLayout.Bands[0].Columns["ID_DonVi"].Hidden = true;
        }
        private void loadComboboxNgoaiTe()
        {
            if (DonVi == 0)
            {
                cboNgoaiTe.DataSource = AccessDataLayer.ExecuteDT("spNgoaiTe_Select", CommandType.StoredProcedure, "@ID_NgoaiTe", null);
                cboNgoaiTe.DisplayLayout.Bands[0].Columns[0].Hidden = true;
               
            }
        }
        private void loadComboboxKhachHang()
        {
            if (DonVi == 0)
            {
                cboKhachHang.DataSource = AccessDataLayer.ExecuteDT("spKhachHang_Select", CommandType.StoredProcedure, "@ID_KhachHang", null);
                cboKhachHang.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                cboKhachHang.DisplayLayout.Bands[0].Columns[2].Hidden = true;
                cboKhachHang.DisplayLayout.Bands[0].Columns[5].Hidden = true;
                cboKhachHang.DisplayLayout.Bands[0].Columns[6].Hidden = true;
                cboKhachHang.DisplayLayout.Bands[0].Columns[7].Hidden = true;
                cboKhachHang.DisplayLayout.Bands[0].Columns[9].Hidden = true;
            }
        }
        private void loadComboboxHangHoa()
        {
            var colHangHoa = new[] { "ID_HangHoa", "Ten_HangHoa", "GiaBan1", "GiaBan2", "GiaBan3" };
            tbHangHoa = AccessDataLayer.ExecuteDT("spHangHoa_Select", CommandType.StoredProcedure, "@ID_HangHoa", null)
                .DefaultView.ToTable(true, colHangHoa);
            tbHangHoa.Columns["Ten_HangHoa"].ColumnName = "Hàng hóa";
            tbHangHoa.Columns["GiaBan1"].ColumnName = "Giá 1";
            tbHangHoa.Columns["GiaBan2"].ColumnName = "Giá 2";
            tbHangHoa.Columns["GiaBan3"].ColumnName = "Giá 3";

            //-----------
            if (DonVi == 0)
            {
                var colThanhPham = new[] { "ID_ThanhPham", "Ten_ThanhPham", "GiaBan1", "GiaBan2", "GiaBan3" };
                tbThanhPham = AccessDataLayer.ExecuteDT("spThanhPham_Select", CommandType.StoredProcedure, "@ID_ThanhPham", null)
                    .DefaultView.ToTable(true, colThanhPham);
                tbThanhPham.Columns["Ten_ThanhPham"].ColumnName = "Thành phẩm";
                tbThanhPham.Columns["GiaBan1"].ColumnName = "Giá 1";
                tbThanhPham.Columns["GiaBan2"].ColumnName = "Giá 2";
                tbThanhPham.Columns["GiaBan3"].ColumnName = "Giá 3";

                //------------
                var colCombo = new[] { "ID_Combo", "Ten_Combo", "Gia_Combo" };
                tbCombo = AccessDataLayer.ExecuteDT("spCombo_Select", CommandType.StoredProcedure, "@ID_Combo", null)
                    .DefaultView.ToTable(true, colCombo);
                tbCombo.Columns["Ten_Combo"].ColumnName = "Combo";
                tbCombo.Columns["Gia_Combo"].ColumnName = "Giá";
            }
            cboHangHoa.DataSource = tbHangHoa;
            cboHangHoa.ValueMember = "ID_Hanghoa";
            cboHangHoa.DisplayMember = "Hàng hóa";
            cboHangHoa.DisplayLayout.Bands[0].Columns[0].Hidden = true;
        }
        private int SaveData()
        {
            int kq = 0;
            if (DonVi == 0)
            {
                 kq = AccessDataLayer.ExecuteNonQuery("spPhieu_TraKH_Insert",
                   CommandType.StoredProcedure,
              "@Ma_PhieuTra_KH", Ma_PhieuTraKH,
              "@ID_KhachHang", cboKhachHang.Value,
              "@ID_TheTraTruoc", myProgress.ID_TheTraTruoc,
              "@ID_ChiNhanh", myProgress.ID_ChiNhanh,
              "@ID_NgoaiTe", cboNgoaiTe.Value,
              "@NgayTra", dtNgayTra.DateTime,
              "@NgayLamViec", myProgress.NgayLamViec,
              "@TongTien", txtTongTien.Text,
              "@GiamGia", txtGiamGia.Value,
              "@Giam_PhanTram", txtGiamPhantram.Value,
              "@ID_KhuVuc_Chitiet", "1",
              "@GhiChu", txtGhiChu.Text,
              "@TinhTrang", myProgress.TinhTrang,
              "@PrintCount", myProgress.PrintCount,
              "@ID_NhanVien", myProgress.ID_NhanVien,
              "@tbPhieu_TraKH_CT", grdPhieu_TraKH_Chitiet.DataSource
              );

            }
            return kq;
           
        }
        private void loadUltraOption()
        {
            ultraOptionHangHoa.CheckedIndex = 0;
        }
        private void SavetoGridKH(string ValueMember, object Value, string Ten_HangHoa, string SoLo, double DonGia, int ID_KhuyenMai, int status, float SoLuong)
        {
            if (SoLo == "")
                SoLo = myProgress.SoLo;
            UltraGridRow urow = grdPhieu_TraKH_Chitiet.Rows.Band.AddNew();
            urow.Cells[ValueMember].Value = Value;
            urow.Cells["Ten_HangHoa"].Value = Ten_HangHoa;
            urow.Cells["SoLuong"].Value = SoLuong;
            urow.Cells["DonGia"].Value = DonGia;
            urow.Cells["ThanhTien"].Value = SoLuong * DonGia;
            urow.Cells["ID_KhuyenMai"].Value = ID_KhuyenMai;
            urow.Cells["Status"].Value = status;
            urow.Cells["SoLo"].Value = SoLo;
            grdPhieu_TraKH_Chitiet.PerformAction(UltraGridAction.CommitRow);

        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (SaveData() > 0)
            {
                MessageBox.Show("Lưu thành công");
            }
        }

        private void grdPhieu_TraKH_AfterRowActivate(object sender, EventArgs e)
        {

            Ma_PhieuTraKH = grdPhieu_TraKH.Rows[this.grdPhieu_TraKH.ActiveRow.Index].Cells["Ma_PhieuTra_KH"].Text;
            txtMa_Phieu_TraKH.Text = Ma_PhieuTraKH;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            loadGridTraHang();
        }
        private void ChangeType_HangHoa()
        {
            int type = ultraOptionHangHoa.CheckedIndex;
            ultraOptionHangHoa.Visible = true;
            cboHangHoa.Text = "";
            if (type == 2)
            {
                ultraOptionHangHoa.CheckedIndex = 0;
                lblHangHoa.Text = ultraOptionHangHoa.Items[0].DisplayText + " :";

                cboHangHoa.DataSource = tbHangHoa;
                cboHangHoa.ValueMember = "ID_Hanghoa";
                cboHangHoa.DisplayMember = "Hàng hóa";

            }
            else if (type == 0)
            {
                ultraOptionHangHoa.CheckedIndex = 1;
                lblHangHoa.Text = ultraOptionHangHoa.Items[1].DisplayText + " :";

                cboHangHoa.DataSource = tbThanhPham;
                cboHangHoa.ValueMember = "ID_ThanhPham";
                cboHangHoa.DisplayMember = "Thành phẩm";
            }
            else
            {
                ultraOptionHangHoa.CheckedIndex = 2;
                lblHangHoa.Text = ultraOptionHangHoa.Items[2].DisplayText + " :";


                cboHangHoa.DataSource = tbCombo;
                cboHangHoa.ValueMember = "ID_Combo";
                cboHangHoa.DisplayMember = "Combo";
            }
            cboHangHoa.DisplayLayout.Bands[0].Columns[0].Hidden = true;
        }

        private void cboHangHoa_KeyDown(object sender, KeyEventArgs e)
        {
           
                if (e.KeyData == Keys.F1)
                {
                    if (DonVi == 0)
                    ChangeType_HangHoa();
                }
                if (e.KeyData == Keys.Enter)
                {
                    ultraOptionHangHoa.Visible = false;
                    if (cboHangHoa.ActiveRow != null)
                    {
                        try
                        {
                            txtDonGia.Value = decimal.Parse(((DataTable)cboHangHoa.DataSource).Rows[cboHangHoa.ActiveRow.Index][2].ToString());
                            txtSoLuong.Focus();
                        }
                        catch (Exception ex)
                        {
                            ErrorScreen er = new ErrorScreen(ex.Message);
                            er.Show();
                        }
                    }
                }
        }
        private void txtDonGia_KeyDown(object sender, KeyEventArgs e)
        {
            if (DonVi == 0)
            {
                if (e.KeyData == Keys.Enter)
                {
                    this.SavetoGridKH(cboHangHoa.ValueMember, cboHangHoa.Value, cboHangHoa.Text,
                        txtSoLo.Text, double.Parse(txtDonGia.Value.ToString()), 1, 1, float.Parse(txtSoLuong.Value.ToString()));
                    cboHangHoa.Focus();
                }
            }
        }

        private void cboNgoaiTe_ValueChanged(object sender, EventArgs e)
        {
            if (DonVi == 0)
            {
                cboKhachHang.Focus();
            }
        }



    











        

    }
}